package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.ComponentMapper;
import com.jl.mapper.DeviceComponentMapper;
import com.jl.mapper.ModelComponentMapper;
import com.jl.model.Component;
import com.jl.model.DeviceComponent;
import com.jl.model.ModelComponent;
import com.jl.model.in.ComponentIn;
import com.jl.service.IComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 零件表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class ComponentServiceImpl extends ServiceImpl<ComponentMapper, Component> implements IComponentService {

    @Autowired
    private ModelComponentMapper partComponentMapper;

    @Autowired
    private DeviceComponentMapper deviceComponentMapper;

    @Override
    public void selectComponentDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<Component> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public void selectComponentDataGridByModelId(PageDataInfo<Map<String, Object>> pageDataInfo, Long modelId) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectComponentDataGridByModelId(page,modelId);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public boolean deleteComponentByIds(List<Long> ids) {
        //删除part_component中的数据
        QueryWrapper<ModelComponent> partComponentQueryWrapper = new QueryWrapper<>();
        partComponentQueryWrapper.in("component_id",ids);
        partComponentMapper.delete(partComponentQueryWrapper);
        //删除device_component中的数据
        QueryWrapper<DeviceComponent> deviceComponentQueryWrapper = new QueryWrapper<>();
        deviceComponentQueryWrapper.in("component_id",ids);
        deviceComponentMapper.delete(deviceComponentQueryWrapper);
        //删除自身
        return baseMapper.deleteBatchIds(ids)>0;
    }

    @Override
    public List<Component> selectComponentByIds(List<Long> ids) {
        return baseMapper.selectComponentByIds(ids);
    }

    @Override
    public boolean isComponentNumberUsed(String number) {
        return baseMapper.selectComponentNumberCount(number)>0;
    }

    @Override
    public boolean isComponentNumberCanUse(String number, Long componentId) {
        ComponentIn componentIn = baseMapper.selectComponentInByNumber(number);
        if(componentIn == null){
            //如果找不到数据则表示该数据没有被使用
            return true;
        }else{
            if(componentIn.getId().equals(componentId)){
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isComponentNameUsed(String name) {
        return baseMapper.selectComponentNameCount(name)>0;
    }

    @Override
    public boolean isComponentNameCanUse(String name, Long componentId) {
        ComponentIn componentIn = baseMapper.selectComponentInByName(name);
        if(componentIn == null){
            //如果找不到数据则表示该数据没有被使用
            return true;
        }else{
            if(componentIn.getId().equals(componentId)){
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }
}
